#include "wx/wxprec.h"
#if wxUSE_XRC && wxUSE_HTML

#include "wx/xrc/xh_htmllbox.h"
#include <wx/htmllbox.h>
#include <wx/filesys.h>

IMPLEMENT_DYNAMIC_CLASS( wxSimpleHtmlListBoxXmlHandler, wxXmlResourceHandler )

wxSimpleHtmlListBoxXmlHandler::wxSimpleHtmlListBoxXmlHandler()
  : wxXmlResourceHandler(), m_insideBox( false ) {
  XRC_ADD_STYLE( wxHLB_DEFAULT_STYLE );
  XRC_ADD_STYLE( wxHLB_MULTIPLE );
  AddWindowStyles();
}

wxObject *wxSimpleHtmlListBoxXmlHandler::DoCreateResource() {
  if( m_class == wxT( "wxSimpleHtmlListBox" ) ) {
    // find the selection
    long selection = GetLong( wxT( "selection" ), -1 );
    // need to build the list of strings from children
    m_insideBox = true;
    CreateChildrenPrivately( NULL, GetParamNode( wxT( "content" ) ) );
    m_insideBox = false;
    XRC_MAKE_INSTANCE( control, wxSimpleHtmlListBox )
    control->Create( m_parentAsWindow,
                     GetID(),
                     GetPosition(), GetSize(),
                     strList,
                     GetStyle( _T( "style" ), wxHLB_DEFAULT_STYLE ),
                     wxDefaultValidator,
                     GetName() );
    if( selection != -1 ) {
      control->SetSelection( selection );
    }
    SetupWindow( control );
    strList.Clear();    // dump the strings
    return control;
  } else {
    // on the inside now.
    // handle <item>Label</item>
    // add to the list
    wxString str = GetNodeContent( m_node );
    if( m_resource->GetFlags() & wxXRC_USE_LOCALE ) {
      str = wxGetTranslation( str, m_resource->GetDomain() );
    }
    strList.Add( str );
    return NULL;
  }
}

bool wxSimpleHtmlListBoxXmlHandler::CanHandle( wxXmlNode *node ) {
  return ( IsOfClass( node, wxT( "wxSimpleHtmlListBox" ) ) || ( m_insideBox && node->GetName() == wxT( "item" ) ) );
}

#endif
